﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ManagerClinic.Core.Common;
using ManagerClinic.Core.Business;
using ManagerClinic.Modules;
using ManagerClinic.Ultilities.Ultility;

namespace ManagerClinic.DanhMuc
{
    public partial class frmCapNhatDmVaTu : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmvattuevent;
        int State = 0;
        DmVatTuBusiness objdmvattubusine = new DmVatTuBusiness();
        DmVatTuEntity objdmvattu = null;
        ThietLapMaBusiness objkey = new ThietLapMaBusiness();
        DmHangSanXuatBusiness objhangsxbusine = new DmHangSanXuatBusiness();
        DmNhomVatTuBusiness objnhomvtbusine = new DmNhomVatTuBusiness();
        DmPhanLoaiVatTuBusiness objdmPloaivtbusine = new DmPhanLoaiVatTuBusiness();
        DmCachDungBusiness objdmcachdungbusine = new DmCachDungBusiness();
        DmDonViBusiness objdonvibusine = new DmDonViBusiness();
        DmGhiChuCachDungBussiness objdmgccdungbusine = new DmGhiChuCachDungBussiness();
        public frmCapNhatDmVaTu(int S)
        {
            InitializeComponent();
            State = S;
            LoadDM();
            txtMavatu.Text = objkey.GetThietLapMa(ThietLapMaEntity.MaVattu);
            txtTenvattu.Focus();
        }
        public frmCapNhatDmVaTu(int S, DmVatTuEntity obj)
        {
            InitializeComponent();
            State = S;
            LoadDM();
            objdmvattu = obj;
            txtMavatu.Enabled = false;
            txtMavatu.Text = objdmvattu.MaVatTu;
            txtTenvattu.Text = objdmvattu.TenVatTu;
            lkHangsx.EditValue = objdmvattu.MaHangSanXuat;
            txtTengoc.Text = objdmvattu.TenGoc;
            lknhomvt.EditValue = objdmvattu.MaNhomVatTu;
            lkPloaivt.EditValue = objdmvattu.MaPhanLoaiVatTu;
            txtthanhphan.Text = objdmvattu.ThanhPhan;
            txtHamluong.Text = objdmvattu.HamLuong;
            lkdonvi.EditValue = objdmvattu.MaDonViDung;
            txtsolandung.Text = objdmvattu.LanDung.ToString();
            txtsoluong.Text = objdmvattu.SoLuongDung.ToString();
            lkcachdung.EditValue = objdmvattu.MaCachDung;
            lkghichucachdung.EditValue = objdmvattu.MaGhiChuCachDung;
            txthuongdansd.Text = objdmvattu.HuongDanSuDung;
            chktrangthaisudung.Checked = objdmvattu.TrangThaiSuDung;
            txtTenvattu.Focus();
        }

        void LoadDMHangSX()
        {
            lkHangsx.Properties.DataSource = objhangsxbusine.GetDmHangSanXuatList();
            lkHangsx.Properties.DisplayMember = "TenHang";
            lkHangsx.Properties.ValueMember = "MaHang";
        }
        void LoadDMNhomVT()
        {
            lknhomvt.Properties.DataSource = objnhomvtbusine.GetDmNhomVatTuList();
            lknhomvt.Properties.ValueMember = "MaNhomVatTu";
            lknhomvt.Properties.DisplayMember = "TenNhomVatTu";
        }
        void LoadLoaiVT()
        {
            lkPloaivt.Properties.DataSource = objdmPloaivtbusine.GetDmPhanLoaiVatTuList();
            lkPloaivt.Properties.ValueMember = "MaPhanLoaiVatTu";
            lkPloaivt.Properties.DisplayMember = "TenPhanLoaiVatTu";
        }
        void LoadDMCachdung()
        {
            lkcachdung.Properties.DataSource = objdmcachdungbusine.GetDmCachDungList();
            lkcachdung.Properties.DisplayMember = "TenCachDung";
            lkcachdung.Properties.ValueMember = "MaCachDung";
        }
        void LoadDMdonvi()
        {
            lkdonvi.Properties.DataSource = objdonvibusine.GetDmDonViList();
            lkdonvi.Properties.DisplayMember = "TenDonVi";
            lkdonvi.Properties.ValueMember = "MaDonVi";
        }
        void LoadDMGhichuCD()
        {
            lkghichucachdung.Properties.DataSource = objdmgccdungbusine.GetDmGhiChuCachDungList();
            lkghichucachdung.Properties.ValueMember = "MaGhiChuCachDung";
            lkghichucachdung.Properties.DisplayMember = "TenGhiChuCachDung";
        }
        void LoadDM()
        {
            LoadDMHangSX();
            LoadDMNhomVT();
            LoadLoaiVT();
            LoadDMCachdung();
            LoadDMdonvi();
            LoadDMGhichuCD();
        }

        private void frmcapnhatDMVatu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkHangsx, "Hãng sản xuất") == false) return;
            if (Validation.ValidLookUpEdit(lknhomvt, "Nhóm vật tư") == false) return;
            if (Validation.ValidLookUpEdit(lkPloaivt, "Phân loại vật tư") == false) return;
            if (Validation.ValidLookUpEdit(lkdonvi, "Đơn vị dùng") == false) return;
            if (Validation.ValidLookUpEdit(lkcachdung, "Cách dùng") == false) return;
            if (Validation.ValidLookUpEdit(lkghichucachdung, "Ghi chú cách dùng") == false) return;
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidIsnumber(txtsolandung, "Số lần dùng") == false) { return; }
            if (Validation.ValidIsnumber(txtsoluong, "Số lượng dùng") == false) { return; }
            if (Validation.ValidSpaceTextBoxKey(txtMavatu, "Mã vật tư") == false) return;
            objdmvattu = new DmVatTuEntity()
            {
                MaVatTu = txtMavatu.Text.Trim(),
                TenVatTu = txtTenvattu.Text.Trim(),
                TenGoc = txtTengoc.Text.Trim(),
                MaHangSanXuat = lkHangsx.EditValue.ToString(),
                MaNhomVatTu = lknhomvt.EditValue.ToString(),
                MaPhanLoaiVatTu = lkPloaivt.EditValue.ToString(),
                ThanhPhan = txtthanhphan.Text.Trim(),
                HamLuong = txtHamluong.Text.Trim(),
                MaDonViDung = lkdonvi.EditValue.ToString(),
                SoLuongDung = double.Parse(txtsoluong.Text),
                LanDung = double.Parse(txtsolandung.Text),
                MaCachDung = lkcachdung.EditValue.ToString(),
                MaGhiChuCachDung = lkghichucachdung.EditValue.ToString(),
                HuongDanSuDung = txthuongdansd.Text.Trim(),
                TrangThaiSuDung = (bool)chktrangthaisudung.Checked

            };
            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (objdmvattubusine.GetDmVatTu(txtMavatu.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMavatu, "Mã vật tư") == false) return;

                objdmvattubusine.InsertDmVatTu(objdmvattu);
                Validation.ClearControl(this);
                txtMavatu.Text = objkey.GetThietLapMa(ThietLapMaEntity.MaVattu);
                txtTenvattu.Focus();
            }
            else
            {
                objdmvattubusine.EditDmVatTu(objdmvattu);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmvattuevent != null)
                dmvattuevent(sender, e);
        }
      
    }
}